package com.xncoding.design.ZeRenLian.demo4;

import java.util.ArrayList;
import java.util.List;

public class FilterChainImpl implements FilterChain {

    private List<Filter> filterList = new ArrayList();
    private Method method;
    private int index = 0;

    FilterChainImpl(Method method) {
        this.method = method;
        filterList.add(new AfterFilter());
        filterList.add(new BeforeFilter());
    }

    @Override
    public void process() {
        if (index < filterList.size()) {
            nextFilter().process(this, method);
        } else {
            method.process_direct();
        }
    }

    @Override
    public Filter nextFilter() {
        return filterList.get(index++);
    }
}
